package org.example.memora.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.example.memora.DTO.UserDTO;
import org.example.memora.DTO.UserLoginDTO;
import org.example.memora.DTO.UserRedisDTO;
import org.example.memora.common.result.Result;
import org.example.memora.common.utils.UserHolder;
import org.example.memora.entity.User;
import org.example.memora.entity.vo.UserInfoVo;
import org.example.memora.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import java.security.cert.CertPathBuilderResult;
import java.util.List;
@Slf4j
@RestController
@RequestMapping("/api/users")
@Tag(name = "用户管理", description = "提供用户的注册、登录、查询、更新、删除等接口")
public class UserController {

    @Autowired
    private UserService userService;
    @Operation(summary = "根据ID获取用户", description = "根据用户ID获取用户详情")
    @Parameter(name = "id", description = "用户ID", required = true)
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查找成功"),
            @ApiResponse(responseCode = "400", description = "请求参数错误"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @GetMapping("/{id}")
    public Result<?> getUserById(@PathVariable Long id) {

        User user=userService.getUserById(id);
        if(user ==null){
            return Result.success();
        }
        return Result.success(user);
    }
    @Operation(summary = "获取当前用户信息", description = "获取当前已登录用户的详细信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "成功获取用户信息"),
            @ApiResponse(responseCode = "401", description = "用户未登录"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @GetMapping("/me")
    public Result<?> me() {
        // 获取当前登录的用户并返回
        UserRedisDTO user = UserHolder.getUser();
        if (user == null) {
            return Result.error("用户未登录");
        }
        Long userId = user.getUserId();
        User userInfo=userService.getUserById(userId);
        UserInfoVo  userInfoVo=new UserInfoVo();
        BeanUtils.copyProperties(userInfo,userInfoVo);
        return Result.success(userInfoVo);
    }

    @PostMapping("/code")
    @Operation(summary = "发送验证码", description = "通过手机号发送短信验证码")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "验证码发送成功"),
            @ApiResponse(responseCode = "400", description = "手机号格式错误"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    public Result<?> sendCode(@RequestParam("phone") String phone) {
        log.debug("发送短信验证码并保存验证码");
        // 发送短信验证码并保存验证码
        return userService.sendCode(phone);
    }
    @PostMapping("/logout")
    @Operation(summary = "退出登录", description = "用户退出登录")
    public Result<?> logout() {
        userService.logout();
        UserHolder.removeUser();
        return Result.success();
    }

    @Operation(summary = "新用户注册", description = "通过用户名和密码注册新用户")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "注册成功"),
            @ApiResponse(responseCode = "400", description = "请求参数错误"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @PostMapping("/register")
    public Result<?> register(@RequestBody UserDTO requestBody) {
        if (requestBody.getUsername() == null || requestBody.getPassword() == null) {
            return Result.error(0, "用户名或密码为空");

        }
        return userService.createUser(requestBody);
    }
    @Operation(summary = "用户登录", description = "用户通过用户名/电话/邮件和密码进行登录")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "登录成功"),
            @ApiResponse(responseCode = "400", description = "请求参数错误"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @PostMapping("/login")
    public Result<?> login(@RequestBody UserLoginDTO requestBody) {
        return userService.login(requestBody);
    }

    @Operation(summary = "获取所有用户", description = "返回系统中所有用户的信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查找成功"),
            @ApiResponse(responseCode = "400", description = "请求参数错误"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @GetMapping
    public Result<?> getAllUsers() {
        List<User> list=userService.getAllUsers();
        if(list.isEmpty()){
            Result.success();
        }
        return Result.success(list);
    }



    @Operation(summary = "编辑用户资料", description = "根据ID更新用户信息")
    @Parameter(name = "id", description = "用户ID", required = true)
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "返回更新后的用户"),
            @ApiResponse(responseCode = "400", description = "请求参数错误"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @PutMapping("/{id}")
    public Result<?> updateUser(@PathVariable Long id, @RequestBody UserDTO userDTO) {
//        User user=userService.updateUser(id, userDTO);
//        if(user==null){
//            return Result.success();
//        }
        return userService.updateUser(id, userDTO);
    }

    @Operation(summary = "注销用户", description = "根据ID删除用户")
    @Parameter(name = "id", description = "用户ID", required = true)
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "删除成功"),
            @ApiResponse(responseCode = "400", description = "请求参数错误"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @DeleteMapping("/{id}")
    public Result<?> deleteUser(@PathVariable Long id) {
        return userService.deleteUser(id);
    }
}
